package ch.bfh.aeneas;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import ch.bfh.aeneas.core.connectivity.RequirementsChecker;
import ch.bfh.aeneas.core.connectivity.PositionAliveKeeper;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import ch.bfh.aeneas.core.io.OutputService;
import ch.bfh.aeneas.core.io.gesture.GestureActivity;
import ch.bfh.aeneas.core.location.PositionManager;
import ch.bfh.aeneas.core.settings.SettingsService;
import ch.bfh.aeneas.core.settings.SettingsService.SettingsServiceBinder;
import ch.bfh.aeneas.exchange.data.Categories;
import ch.bfh.aeneas.utils.AssetsHelper;

/**
 * This the main class of the application.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class AeneasMain extends GestureActivity implements OnClickListener {
    
    private ServiceConnection mSettingsConn = new ServiceConnection() {
        public void onServiceConnected(ComponentName cn, IBinder ib) {
            mSettings = ((SettingsServiceBinder)ib).getService();
            setupEnvironment();
        }

        public void onServiceDisconnected(ComponentName cn) {
            
        }
    };
   
    private static final String LOG_TAG = "AeneasMain";
    private static final int REQUIREMENTS_CHECK = 42;
    
    private Button mAroundBtn;
    private Button mFindBtn;
    private Button mRouteBtn;
    private Button mTrackBtn;
    private Button mPointBtn;
    private Button mExportBtn;
    private Button mSettingsBtn;
    private boolean mEnvironmentSetup;
    private boolean mUseGestures;
    private SettingsService mSettings;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        mUseGestures = sp.getBoolean("use_gestures", false);
        if(mUseGestures) {
            super.onCreate(savedInstanceState, R.layout.main);
        } else {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.main);
        }
        
        mSettingsBtn = (Button) findViewById(R.id.settings_button);
        mSettingsBtn.setOnClickListener(this);
        
        mAroundBtn = (Button) findViewById(R.id.around_button);   
        mAroundBtn.setOnClickListener(this);
        
        mFindBtn = (Button) findViewById(R.id.find_button);
        mFindBtn.setOnClickListener(this);
        
        mRouteBtn = (Button) findViewById(R.id.route_button);
        mRouteBtn.setOnClickListener(this);
        
        mTrackBtn = (Button) findViewById(R.id.track_button);
        mTrackBtn.setOnClickListener(this);
        
        mPointBtn = (Button) findViewById(R.id.point_button);
        mPointBtn.setOnClickListener(this);
        
        mExportBtn = (Button) findViewById(R.id.export_button);
        mExportBtn.setOnClickListener(this);
        
        mEnvironmentSetup = false;
        
        startActivityForResult(new Intent(this, RequirementsChecker.class), REQUIREMENTS_CHECK);
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        
        if(requestCode == REQUIREMENTS_CHECK) {
            if(resultCode == Activity.RESULT_OK) {
                Log.i(LOG_TAG,"before binding Settingsservice");
                bindService(new Intent(this, SettingsService.class), mSettingsConn, Context.BIND_AUTO_CREATE);
            } else {
                
            }
        }
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onDestroy() {
        super.onDestroy();
        unbindService(mSettingsConn);
        
        if(mEnvironmentSetup) {
            Intent stopIntent;
        
            stopIntent = new Intent(this, PositionAliveKeeper.class);
            stopService(stopIntent);
            
            stopIntent = new Intent(this, PositionManager.class);
            stopService(stopIntent);
            
            stopIntent = new Intent(this, OutputService.class);
            stopService(stopIntent);

            stopIntent = new Intent(AeneasIntent.ACTION_MANAGE);
            stopIntent.addCategory(AeneasIntent.CATEGORY_DATA);
            stopService(stopIntent);
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public void onClick(View arg0) {
        Intent i = null;
        if(arg0 == mSettingsBtn) {
            //i = new Intent(this,GestureBuilder.class);
            
            //i = new Intent(this,TextToSpeechActivity.class);
            
            //i = new Intent(this,menu_l1.class);
            
            //i =new Intent(this,MyPrefActivity.class);
            
            //i =new Intent(this,DemoGui.class);
            
            //i =new Intent(this,MultitouchActivity.class);
            
            //i =new Intent(this,GestureBuilderActivity.class);
            
            //i = new Intent(this,Compass.class);
            
            i = new Intent(AeneasIntent.ACTION_SELECT);
            i.addCategory(AeneasIntent.CATEGORY_SETTINGS);
            
        } else if(arg0 == mAroundBtn) {           
            //Intent i = new Intent(this, AroundMe.class);            
            i = new Intent(AeneasIntent.ACTION_AROUND);
            i.addCategory(AeneasIntent.CATEGORY_NAVIGATION);
            
        } else if(arg0 == mFindBtn) {
            //Intent i = new Intent(this, FindMe.class);            
            i = new Intent(AeneasIntent.ACTION_FIND);
            i.addCategory(AeneasIntent.CATEGORY_NAVIGATION);           
            
        } else if(arg0 == mRouteBtn) {
            //Intent i = new Intent(this, RouteMe.class);            
            i = new Intent(AeneasIntent.ACTION_ROUTE);
            i.addCategory(AeneasIntent.CATEGORY_NAVIGATION);
            
        } else if(arg0 == mTrackBtn) {
            //Intent i = new Intent(this, TrackMe.class);            
            i = new Intent(AeneasIntent.ACTION_TRACK);
            i.addCategory(AeneasIntent.CATEGORY_NAVIGATION);
            
        } else if(arg0 == mPointBtn) {
            //Intent i = new Intent(this, TrackMe.class);            
            i = new Intent(AeneasIntent.ACTION_STORE);
            i.addCategory(AeneasIntent.CATEGORY_NODE);
            
        } else if(arg0 == mExportBtn) {
            //Intent i = new Intent(this, ExportMe.class);
            i = new Intent(AeneasIntent.ACTION_OUTPUT);
            i.addCategory(AeneasIntent.CATEGORY_DATA);
            i.putExtra("kml",true);
        }
        if(i != null) {
            i.putExtra("useGestures", mUseGestures);
            startActivity(i);
        }
    }
    
    protected void setupEnvironment() {
        Categories.setup(this);
        
        AssetsHelper.copyAssets(this, "/mnt/sdcard/aeneas/");
        
        Intent startIntent;
        
        startIntent = new Intent(this, PositionManager.class);
        startService(startIntent);
        
        startIntent = new Intent(this, PositionAliveKeeper.class);
        startIntent.putExtra("listen", true);
        startService(startIntent);
        
        startIntent = new Intent(this, OutputService.class);
        startService(startIntent);
        
        startIntent = new Intent(AeneasIntent.ACTION_MANAGE);
        startIntent.addCategory(AeneasIntent.CATEGORY_DATA);
        startService(startIntent);
        //bindService(startIntent, mDataConn, Context.BIND_AUTO_CREATE);
        
        mEnvironmentSetup = true;
    }
}
